package cn.uniquesoft.controller;

import java.io.File;
import java.util.Date;

import cn.uniquesoft.service.ISearchService;
import cn.uniquesoft.util.ContextUtil;
import cn.uniquesoft.vo.SearchVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.uniquesoft.controller.base.BaseController;
import cn.uniquesoft.service.IBankService;
import cn.uniquesoft.util.DateUtil;
import cn.uniquesoft.util.FileUpUtil;
import cn.uniquesoft.vo.AnswerVO;
import cn.uniquesoft.vo.BankVO;
import cn.uniquesoft.vo.BankVO;
import cn.uniquesoft.vo.base.BaseVO;

@Controller
@RequestMapping(value = "/bank")
public class BankController extends BaseController<BankVO> {
    private IBankService bankService;
    @Autowired
    private ISearchService searchService;
    public BankController(IBankService bankService) {
        super();
        this.bankService = bankService;
        this.baseService = bankService;
        this.indexName = "bank/index";
    }

    @RequestMapping(value = "/save")
    @ResponseBody
    public AnswerVO save(BankVO item) {
        AnswerVO answerVO = new AnswerVO();
        try {
            MultipartFile file = item.getUpfile();
            if (file.getSize() > 0) {
                String tomurl = ContextUtil.getRequest().getSession().getServletContext().getRealPath("/");
                // 获得上传文件原始名
                String oldname = file.getOriginalFilename();
                // 算出后缀名
                String ext = oldname.substring(oldname.lastIndexOf("."));
                // 文件名为：问题名称 + image + UUID + 后缀
                String fileName = "bank" + DateUtil.date2timestamp(new Date(), DateUtil.FORMAT_DATETIME) + ext;
                String filepath = "files" + File.separator + "bank" + File.separator;
                String fileFolder = tomurl + filepath;
                File upload = new File(fileFolder);
                if (!upload.exists()) upload.mkdirs();
                File localFile = new File(fileFolder, fileName);

                // 直接写入到后台服务器，简单且快
                file.transferTo(localFile);
                item.setCpicname(oldname);
                item.setCpicpath(fileName);
                item.setCpicsize(FileUpUtil.convertFileSize(file.getSize()));
            }
            this.bankService.save(item);
            int itemId = ((BaseVO) item).getIid();
            if (itemId > 0) {
                SearchVO searchVO = new SearchVO();
                searchVO.setTname("bank");
                searchVO.setTid(item.getIid()+"");
                searchVO.setTvalue(item.toString());
                System.out.println(item.toString());
                searchVO.setTtitle(item.getCname());
                searchVO.setId(searchVO.getTid() + "_" + searchVO.getTname());
                this.searchService.save(searchVO);
                answerVO.setSuccess(true);
                answerVO.setMsg("新增成功!");
            } else {
                answerVO.setSuccess(false);
                answerVO.setMsg("新增失败!");
            }
            answerVO.setData(itemId);
        } catch (Exception e) {
            e.printStackTrace();
            answerVO.setSuccess(false);
            answerVO.setMsg("新增失败," + e.getMessage());
        }
        return answerVO;
    }

    @RequestMapping(value = "/edit")
    @ResponseBody
    public AnswerVO edit(BankVO item) {
        AnswerVO answerVO = new AnswerVO();
        try {
            MultipartFile file = item.getUpfile();
            if (file.getSize() > 0) {
                String tomurl = ContextUtil.getRequest().getSession().getServletContext().getRealPath("/");

                // 获得上传文件原始名
                String oldname = file.getOriginalFilename();
                // 算出后缀名
                String ext = oldname.substring(oldname.lastIndexOf("."));
                // 文件名为：问题名称 + image + UUID + 后缀
                String fileName = "bank" + DateUtil.date2timestamp(new Date(), DateUtil.FORMAT_DATETIME) + ext;
                String filepath = "files" + File.separator + "bank" + File.separator;
                String fileFolder = tomurl + filepath;
                File upload = new File(fileFolder);
                if (!upload.exists()) upload.mkdirs();
                File localFile = new File(fileFolder, fileName);
                // 直接写入到后台服务器，简单且快
                file.transferTo(localFile);
                item.setCpicname(oldname);
                item.setCpicpath(fileName);
                item.setCpicsize(FileUpUtil.convertFileSize(file.getSize()));
            }
            int result = this.bankService.edit(item);
            if (result > 0) {
                this.searchService.remove(item.getIid()+"", "bank");
                SearchVO searchVO = new SearchVO();
                searchVO.setTname("article");
                searchVO.setTid(item.getIid()+"");
                searchVO.setTvalue(item.toString());
                System.out.println(item.toString());
                searchVO.setId(searchVO.getTid() + "_" + searchVO.getTname());
                searchVO.setTtitle(item.getCname());
                this.searchService.save(searchVO);
                answerVO.setSuccess(true);
                answerVO.setMsg("修改成功!");
            } else {
                answerVO.setSuccess(false);
                answerVO.setMsg("修改失败!");
            }
            answerVO.setData(result);
        } catch (Exception e) {
            answerVO.setSuccess(false);
            answerVO.setMsg("修改失败," + e.getMessage());
        }

        return answerVO;
    }
    @RequestMapping(value = "/removeById")
    @ResponseBody
    @Override
    public AnswerVO removeById(int  iid) {
        AnswerVO answerVO = new AnswerVO();
        try {
            int result = this.baseService.removeById(iid);

            if (result > 0) {
                this.searchService.remove(iid+"", "bank");
                answerVO.setSuccess(true);
                answerVO.setMsg("删除成功!");
            } else {
                answerVO.setSuccess(false);
                answerVO.setMsg("删除失败!");
            }
            answerVO.setData(result);
        } catch (Exception e) {
            answerVO.setSuccess(false);
            answerVO.setMsg("删除失败," + e.getMessage());
        }

        return answerVO;
    }

    @RequestMapping(value = "/removeByIds")
    @ResponseBody
    @Override
    public AnswerVO removeByIds(String ids) {
        AnswerVO answerVO = new AnswerVO();
        try {
            int result = this.baseService.removeByIds(ids);

            if (result > 0) {
                String[] ss = ids.split(",");
                for(int i=0;i<ss.length;i++){
                    this.searchService.remove(ss[i]+"", "bank");
                }
                answerVO.setSuccess(true);
                answerVO.setMsg("删除成功!");
            } else {
                answerVO.setSuccess(false);
                answerVO.setMsg("删除失败!");
            }
            answerVO.setData(result);
        } catch (Exception e) {
            answerVO.setSuccess(false);
            answerVO.setMsg("删除失败," + e.getMessage());
        }

        return answerVO;
    }
}
